// module.exports = {
//     publicPath: process.env.NODE_ENV === "development" ? '' : ''
// }

let fs = require('fs')

let projectVersion = {
    version: '1.0.0' // 初始版本
}

let incremental = 1 // 迭代 +0.0.1

let filePath = 'project.version.json'

projectVersion.version = getVersion(filePath,projectVersion)
// projectVersion.version = ''

let version = projectVersion.version

let prodPath = '/' + version + '/'

let isProd = process.env.NODE_ENV === 'production' ? true : false

module.exports = {
    publicPath: isProd ? prodPath : '',
    outputDir: isProd ? 'dist/' + version: 'dist',
    // indexPath: isProd ? '../index.html' : 'index.html'
}

function getVersion(filePath = '',projectVersion = {})
{
    return readFile(filePath,projectVersion).version
}

function readFile(filePath = '',projectVersion = {})
{
    let content = false

    try{
        fs.accessSync(filePath, fs.constants.R_OK)
        
        let data = fs.readFileSync(filePath)
        content = JSON.parse(data.toString())
        if(!content.version) content = projectVersion
        else {
            let text = ''
            let result = ''
            let arr = content.version.replace(new RegExp('@','g'),'').split('.')

            arr.forEach(item => {
                text = text + (item + '')
            })

            text = parseInt(text)
            text += incremental
            text = text + ''

            // console.log('****************')
            for(let i=0;i<arr.length;i++){
                let single = text.substring(i,(i+1))

                if(i === 0) {
                    // result = result +('@'+single)
                    result = result +(''+single)
                } else {
                    result = result + ('.'+single)
                }
            }

            content.version = result

        }

    }catch (err) {
        content = projectVersion
    }

    saveFile(filePath,content)

    return content
}

function saveFile(filePath = '', projectVersion = {}){
    fs.writeFile(filePath, JSON.stringify(projectVersion), (err) => {
        if(err) throw err
        // success
    })
}